using System;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Common.Commands.MemberCommands;
using Common.Views;
using Dto.ApiRequests.MemberForms;
using Dto.ApiResponses;
using Dto.ApiResponses.MemberResponses;
using NServiceBus;
using PetaPoco;
using Sanelib.Common.Helpers;
using Sanelib.DataView;
using Sanelib.Dto;
using WebApp.Services;

namespace WebApp.ControllerApis
{
    public class MemberController : SmartApiController
    {
        private readonly IViewRepository<MemberView> _memberViewRepository;
        public IBus Bus { get; set; }
        public MemberController(IUserSession userSession, IMappingEngine mappingEngine, IViewRepository<MemberView> memberViewRepository) : base(userSession, mappingEngine)
        {
            _memberViewRepository = memberViewRepository;
        }

        [Authorize]
        [ActionName("BySpecification")]
        public HttpResponseMessage GetBySpecification([FromUri] SearchSpecification form)
        {
            var memberviewPage = _memberViewRepository.SearchBySpecification(form, Property.Of<MemberView>(x => x.ModifiedOn));
            var response = Mapper.Map<Page<MemberView>, PageResponse<MemberLine>>(memberviewPage);

            return Content(response);
        }

        [Authorize]
        public HttpResponseMessage Get([FromUri] MemberLookupForm form)
        {
            var searchSpecification = new SearchSpecification { FilterType = "equal" };
            if (form.MemberId.HasValue)
            {
                searchSpecification.ColumnName = "Id";
                searchSpecification.ColumnValue = form.MemberId.Value.ToString();
            }
            else if (!string.IsNullOrEmpty(form.TagId))
            {
                searchSpecification.ColumnName = "TagId";
                searchSpecification.ColumnValue = form.TagId;
            }
            else if (!string.IsNullOrEmpty(form.TagId))
            {
                searchSpecification.ColumnName = "Code";
                searchSpecification.ColumnValue = form.Code;
            }
            else
            {
                var result = new ErrorResult();
                result.AddError("Key", "No value supplied");
                return Content(result);
            }

            var page = _memberViewRepository.SearchBySpecification(searchSpecification, Property.Of<MemberView>(x => x.DateOfBirth));
            var response = Mapper.Map<Page<MemberView>, PageResponse<MemberLine>>(page);
            return Content(response.TotalItems > 0 ? response.Items[0] : null);
        }

        public HttpResponseMessage Post(AddMemberForm form)
        {
            var command = Mapper.Map<AddMemberForm, AddMember>(form);
            return ExecuteCommand(command);
        }

        [Authorize]
        public HttpResponseMessage Put(UpdateMemberForm form)
        {
            var command = MappingEngine.Map<UpdateMemberForm, UpdateMember>(form);
            return ExecuteCommand(command);
        }

        [Authorize]
        public HttpResponseMessage Delete(Guid id)
        {
            var command = new DeleteMember(id);
            return ExecuteCommand(command);
        }
    }
}